package com.spx.chapter08;

import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

/**
 * create by undeRdoG on  2022-05-04  14:30
 * 凡心所向，素履以往，生如逆旅，一苇以航。
 */
public class ConnectTest {

    public static void main(String[] args) throws Exception {

        /*
        * 合流，可以合并两条类型不同的DataStream
        * */
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<Integer> source1 = env.fromElements(1, 2, 3);

        DataStreamSource<Long> source2 = env.fromElements(1L, 2L, 3L);

        ConnectedStreams<Integer, Long> connectStream = source1.connect(source2);


        SingleOutputStreamOperator<String> result = connectStream.map(new CoMapFunction<Integer, Long, String>() {
            @Override
            public String map1(Integer value) throws Exception {
                return String.valueOf(value);
            }

            @Override
            public String map2(Long value) throws Exception {
                return String.valueOf(value);
            }
        });

        result.print("res").setParallelism(1);
        env.execute();
    }
}
